// pages/baogao/baogao.js
import {reqUserInfo} from '../../api/user'
import {ComponentWithStore} from 'mobx-miniprogram-bindings'
import { userStore} from '../../stores/userstore'
import {setStorage} from '../../utils/storage'
ComponentWithStore({
  /**
   * 页面的初始数据
   */
  data: {
    name:'mini66',
    age:48,
    initialWeight:72.6,
    recentWeight: 73,
    targetWeight:70, 
    height: 175,
    bmi: 20,     // BMI
    bmiCategory:'正常' , // BMI分类
  },
  storeBindings:{
    store:userStore,
    fields:['userInfo'],
    actions:['setUserInfo']
  },


  methods:{
    async getWeight(){
      const res = await reqUserInfo()
      console.log(res.data)
      const weightData = res.data
      //将信息存入本地
      setStorage('userInfo',weightData)
      //将信息存储到Store对象
      this.setUserInfo(weightData)
      //响应式渲染数据
      this.setData({
        name:weightData.name,
        age:weightData.age,
        height:weightData.height,
        initialWeight:weightData.initialWeight,
        recentWeight:weightData.currentWeight,
        targetWeight:weightData.targetWeight,
        bmi:weightData.bmi,
        bmiCategory:weightData.bmiCategory
      })
      },
    onLoad: function () {
    this.getWeight()
        },
    onShow: function () {
      this.getWeight()
    }
  },

})